package top.guoyang.springboot3.service.lianxi.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.guoyang.springboot3.aop.AuthInfo;
import top.guoyang.springboot3.base.PageBean;
import top.guoyang.springboot3.base.R;
import top.guoyang.springboot3.dao.TbContact.TbContactMapper;
import top.guoyang.springboot3.entity.TbContact.TbContact;
import top.guoyang.springboot3.interceptor.AppInterceptor;
import top.guoyang.springboot3.service.auth.AuthService;
import top.guoyang.springboot3.service.lianxi.TbContactService;
import top.guoyang.springboot3.vo.PageVO;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbContactServiceImpl implements TbContactService {
    private final TbContactMapper tbContactMapper;

    private final AuthService authService;

    @Override
    public R<String> insert(TbContact tbContact) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();
        tbContact.setUid(authInfo.getUid());
        int i = tbContactMapper.insert(tbContact);
        return i == 1 ? R.ok("添加成功", "") : R.fail("添加失败", "");
    }

    @Override
    public R<PageVO<TbContact>> query(TbContact tbContact, PageBean pageBean) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();

        QueryWrapper<TbContact> wrapper = new QueryWrapper<>();

        wrapper.select("id", "username", "phone", "email", "address");

        wrapper.eq("uid", authInfo.getUid());
        wrapper.like(StringUtils.hasText(tbContact.getUsername()), "username", tbContact.getUsername());
        wrapper.like(StringUtils.hasText(tbContact.getPhone()), "phone", tbContact.getPhone());
        wrapper.orderByDesc("id");
        IPage<TbContact> page = new Page<>(pageBean.getPageNumber(), pageBean.getPageSize());
        page = tbContactMapper.selectPage(page, wrapper);

        return R.ok(page);
    }
    @Override
    public R<String>delete(Integer id){
        AuthInfo authInfo = AppInterceptor.getAuthInfo();

        TbContact  tbContact = tbContactMapper.selectById(id);
        if (tbContact == null) {
            return R.fail("数据不存在", "");
        }
        if (!tbContact.getUid().equals(authInfo.getUid())) {
            return R.fail("只能删除自己的数据", "");
        }
        int i = tbContactMapper.deleteById(id);
        return i == 1 ? R.ok("删除成功", "") : R.fail("删除失败", "");
    }

    @Override
    public R<String> update(TbContact tbContact){
        AuthInfo authInfo = AppInterceptor.getAuthInfo();
        TbContact check = tbContactMapper.selectById(tbContact.getId());
        if (check == null) {
            return R.fail("数据不存在", "");
        }
        if (!check.getUid().equals(authInfo.getUid())) {
            return R.fail("只能更新自己的数据", "");
        }
        int i = tbContactMapper.updateById(tbContact);
        return i == 1 ? R.ok("更新成功", "") : R.fail("更新失败", "");
    }

    @Override
    public R<TbContact> queryById(Integer id){
        TbContact  tbContact = tbContactMapper.selectById(id);
        if (tbContact == null){
            return R.fail("数据不存在", null);
        }

        return R.ok("", tbContact);
    }
}
